﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SharpSxwnl;
using System.Diagnostics.Contracts;
using System.Drawing.Imaging;

namespace 天机万年历.自定义控件
{
    public partial class LunarDay : UserControl
    {
        private DateTime date = DateTime.Now;
        [Category("日历"), Description("当前日期"), DefaultValue(typeof(DateTime), "2019-01-01")]
        public DateTime Date
        {
            get => date;
            set
            {
                if (date != value)
                {
                    date = value;
                    ReLoad();
                }
            }
        }

        public bool 显示农历节日 { get; set; } = true;

        public Color 农历节日颜色 { get; set; } = Color.LawnGreen;

        public bool 显示公历节日 { get; set; } = true;

        public Color 公历节日颜色 { get; set; } = Color.LawnGreen;

        public Color WeekendColor { get; set; } = Color.Red;

        public Color MouseHoverBackColor { get; set; } = Color.AliceBlue;

        private bool dayInMonth = true;
        public bool DayInMonth { get =>dayInMonth; 
            set { 
                if (dayInMonth != value)
                {
                    dayInMonth = value;
                    ReLoad();
                }
            }
        }

        private Lunar lunar = new Lunar();
        public LunarDay()
        {
            InitializeComponent();
        }

        private void LunarDay_Layout(object sender, LayoutEventArgs e)
        {
            ReLoad();
        }

        public void ReLoad()
        {
            lb_天.Text = Date.Day.ToString();
            var ob = lunar.RiCalc(Date);
            var c = "";

            lb_农历或节日.ForeColor = Color.Black;
            if (显示公历节日)
            {
                if (ob.A.Length > 0)
                    c += lunar.substr2(ob.A, 4, "..");
                if (c.Length <= 0 && ob.B.Length > 0)
                    c = lunar.substr2(ob.B, 4, "..");
                if (c.Length > 0)
                    lb_农历或节日.ForeColor = 公历节日颜色;
            }
            if (c.Length <= 0 && ob.Ldc == "初一")
                c = ob.Lleap + ob.Lmc + "月" + (ob.Ldn == 30 ? "大" : "小");
            if (c.Length <= 0)
                c = ob.Ldc;
            lb_农历或节日.Text = c;
            if (DayInMonth)
            {
                if (Date.DayOfWeek == DayOfWeek.Saturday || Date.DayOfWeek == DayOfWeek.Sunday)
                {
                    lb_天.ForeColor = WeekendColor;
                }
                else
                {
                    lb_天.ForeColor = Color.Black;
                }
            }
            else
            {
                lb_天.ForeColor = Color.Gray;
                lb_农历或节日.ForeColor = Color.Gray;
            }
        }

        Color YBGColor;
        private void LunarDay_MouseHover(object sender, EventArgs e)
        {
            YBGColor = this.BackColor;
            this.BackColor = MouseHoverBackColor;
        }

        private void LunarDay_MouseLeave(object sender, EventArgs e)
        {
            this.BackColor = YBGColor;
        }

        private void OnClick(object sender, EventArgs e)
        {
            Contract.Requires(e != null);
            
            ((EventHandler)Events[EventClick])?.Invoke(this, e);

            ControlPrinter.CopyControlToClipboard(this);
        }

        private static readonly object EventClick = new object();
        new public event EventHandler Click
        {
            add
            {
                Events.AddHandler(EventClick, value);
            }
            remove
            {
                Events.RemoveHandler(EventClick, value);
            }
        }

        private bool CustomDraw = false;
        protected override void OnPaint(PaintEventArgs e)
        {
            if (CustomDraw)
            {
                var rect = e.ClipRectangle;
                rect.X = Left;
                rect.Y = Top;
                base.OnPaint(new PaintEventArgs(e.Graphics, rect));
            }
            else
                base.OnPaint(e);
        }

        public void Draw(Graphics g)
        {
            this.lb_天.CustomPaint(g);
            this.lb_农历或节日.CustomPaint(g);
        }
    }
}
